package com.zhongwang.business.module.baseCostManage.controller;
import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.gen.entity.User;
import com.zhongwang.business.module.baseCostManage.model.InputExtProPlanModel;
import com.zhongwang.business.module.baseCostManage.service.InputExtProPlanService;
import com.zhongwang.business.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *  控制层
 * @author China.kxx
 * @date 2020/10/26 18:31
 */
@Slf4j
@Controller
@RequestMapping("/dataEntry/extProPlan")
public class InputExtProPlanController extends BaseController {


    //页面文件路径
    private String prefix = "/dataEntry";
    @Autowired
    private InputExtProPlanService inputExtProPlanService;
    //加载页面
    @GetMapping("/manpower")
    public String manpower(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/manpower";
    }


    @GetMapping
    public String page(Model model) {
        model.addAttribute("categories", inputExtProPlanService.findTypes());
        return "dataEntry/extProPlan";
    }


    /**
     * 加载外协列表数据
     * @param inputExtProPlanModel
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    public JsonResult manpower(InputExtProPlanModel inputExtProPlanModel) {
        List<InputExtProPlanModel> list = inputExtProPlanService.inputExtProPlan(inputExtProPlanModel);
        return jsonResult(list);
    }
    /**
     * 导入
     * @param file 导入文件
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/import")
    public JsonResult inputOutsourcingPlan(@RequestParam("file") MultipartFile file, InputExtProPlanModel inputExtProPlanModel) {
        return success(inputExtProPlanService.importFile(file, inputExtProPlanModel));
    }
    /**
     * 错误文件导出
     * @param url 文件路径
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/exist")
    public JsonResult checkFileExist(String url) {
        return jsonResult(inputExtProPlanService.exist(url));
    }

    /**
     * 原材料计划数据清空
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/delExtPro")
    public JsonResult delExtPro(InputExtProPlanModel inputExtProPlanModel) {
        int row = inputExtProPlanService.delExtPro(inputExtProPlanModel);
        if (row == -1){
            return error("外购件计划数据已被接收，无法进行删除！！需联系商务科");
        }else if(row == 0){
            return error("外购件计划无数据，操作失败");
        }else if(row == 1){
            return jsonResult(inputExtProPlanService.deleteExtPro(inputExtProPlanModel));
        }else {
            return JsonResult.fail();
        }
    }
}